<?php
class UserController extends Core_Controller
{
    public function indexAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Quản lý thành viên";
        $this->_view->activeClass = "user";
    }
    
    public function pagingAction()
    {
    	if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
    	$this->_view->layout(false);
    	
    	if(count($this->_http->getArvs()) != 1) 
    		$this->_view->status = 0;
    	else 
    	{
    		$p = $this->_http->getParam('p');
	        if($p == null) 
	        	$this->_view->status = 0;
	        else 
	        {
	        	$p = intval($p);
	        		        
	       		$modelUser = new Admin_Model_User();
	        	$listUser = $modelUser->getUsers();
	           
		        if($listUser != null)
		        {
		        	$paginator = new Utilities_Paginator($listUser);
		        	$paginator->setItemCountPerPage(15);
		        	$paginator->setCurrentPageNumber($p);
		        	$this->_view->list = $paginator->getResult();
		        	$this->_view->paginator = $paginator;
		        	$this->_view->indexStart = ($paginator->getCurrentPageNumber()-1) * $paginator->getItemCountPerPage() + 1;
		        	$this->_view->status = 1;
		        }
		        else 
		        	$this->_view->status = 0;
	        }
    	}
    	
    }
    
    public function createAction()
    {
        if(count($this->_http->getArvs())>0) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Thêm thành viên";
        $this->_view->activeClass = "user";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $u  = '';
        $p  = '';
        $rp = '';
        $fn = '';
        $e  = '';
        $cd = 0;
        $ban= 0;
        
        if(isset($_POST['username']))
        {
            $u = trim($_POST['username']);
            $p = trim($_POST['password']);
            $rp = trim($_POST['repassword']);
            $fn = trim($_POST['fullname']);
            $e = trim($_POST['email']);
            $cd = trim($_POST['credit']);
            $ban = trim($_POST['ban']);
            $status = 1;
            
            if(($u == "") || ($p == "") || ($rp == "")  || ($fn == "")  || ($e == "")  || ($cd == "") || ($ban == ""))
            {
               $message = " Chưa nhập đủ thông tin";    
            } 
            else
               if($p !== $rp)
               {
                    $message = "Mật khẩu không khớp ";  
               }
                else
                {
                	$modelUser1 = new Model_User();
        			if($modelUser1->checkUsername($u))
        			{
        	  		  $message = "Tên tài khoản đã tồn tại.";
        			}
        			else
        			     if($modelUser1->checkEmail($e))
    			         {
        	   			  $message = "Tên email đã tồn tại.";
        				 }
        				 else
        				 {
			               			$userApi  = new Api_User();
			        			$user_dir = $userApi->createUser($u, $e);
			        			if($user_dir != "")
			        			{
			 						$modelUser = new Admin_Model_User();
			        				$ok = $modelUser->insertUser($u,$p,$fn,$e,$cd,$ban,$user_dir);
			        			}
			        		  	else
			        		  	    $ok = false;
			        		  	
			        		  	/*
			        		  	    $user_dir = "";
			        		  	    $modelUser = new Admin_Model_User();
			        				$ok = $modelUser->insertUser($u,$p,$fn,$e,$cd,$ban,$user_dir);    */
			        		  	
			                    if(! $ok )
			                       $message = " Thêm thành viên thất bại do lỗi hệ thống";
			                    else
			                    {
			                        $status = 2;
			                        $this->redirect('/admin/user');
			                    }
        				 }  
                }
        }
        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_username = $u;
        $this->_view->input_fullname = $fn;
        $this->_view->input_email = $e;
        $this->_view->input_credit = $cd;
        $this->_view->input_ban = $ban; 
    }
    
    public function editAction()
    {
      	if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        $this->_view->title = "Sửa thông tin thành viên";
        $this->_view->activeClass = "user";
        
        //status  0: chua submit , 1: da submit va co loi , 2: submit thanh cong
        $status = 0;
        $message = '';
        $u  = '';
        $p  = '';
        $rp = '';
        $fn = '';
        $e  = '';
        $cd = 0;
        $ban= 0;
        
         	$modelUser = new Admin_Model_User();
            $result = $modelUser->getUser($id);
            if($result == null) throw new Exception("404 not found",404);
            $u = $result[0]['username'];
            $p = $result[0]['password'];
            $fn = $result[0]['fullname'];
            $e  = $result[0]['email'];
            $cd = $result[0]['credit'];
            $ban = $result[0]['ban'];
        
        if(isset($_POST['fullname']))
        {
           
            $p = trim($_POST['password']);
            $rp = trim($_POST['repassword']);
            $fn = trim($_POST['fullname']);
           
            $cd = trim($_POST['credit']);
            $ban = trim($_POST['ban']);
            $status = 1;
            
            if(($fn == "") || ($cd == "") || ($ban == ""))
            {
               $message = " Chưa nhập đủ thông tin";    
            } 
            else
               if((($p != "")||($rp != "")) && ($p !== $rp))
               {
                    $message = "Mật khẩu không khớp ";  
               }
                else
                {
                	   		  	$modelUser = new Admin_Model_User();
			        			$ok = $modelUser->updateUser($id,$fn, $cd, $ban, $p);    
			        		  	
			                    if(! $ok )
			                       $message = " Sửa thông tin thành viên thất bại do lỗi hệ thống";
			                    else
			                    {
			                        $status = 2;
			                        $this->redirect('/admin/user');
			                    }
        				 
                }
        }

        
        $this->_view->status = $status;
        $this->_view->message = $message;
        $this->_view->input_username = $u;
        $this->_view->input_fullname = $fn;
        $this->_view->input_email = $e;
        $this->_view->input_credit = $cd;
        $this->_view->input_ban = $ban; 
        $this->_view->id = $id; 
    }
    
    public function deleteAction()
    {
        if(count($this->_http->getArvs()) != 1 ) throw new Exception("404 not found",404);
        $id = $this->_http->getParam('id');
        if($id == null) throw new Exception("404 not found",404);
        if(! isset($_SESSION['admin_id'])) $this->redirect('/admin');
        
        $modelUser = new Admin_Model_User();
        $result = $modelUser->getUser($id);
        if($result != null)
        {
        
	        //xoa cac may ao su dung boi user nay
	        $modelVm = new Model_Vm();
	        $vm_info = $modelVm->getVms($id);
	        if($vm_info != null)
	        {
	        	$countVms = count($vm_info);
	        	$apiVm    = new Api_Vm();
	        	for($k = 0;$k < $countVms; $k++)
	        	{
	        		if($vm_info[$k]['status'] == 1)
	        			$apiVm->stopVm($vm_info[$k]['default_name']);
	           		$apiVm->deleteVm($vm_info[$k]['username'],$vm_info[$k]['vmname']);
	           		$modelVm->deleteVm($vm_info[$k]['vm_id']);
	        	}
	        }
	        
	        
	        
	        //goi shell xoa user nay
	        $u = $result[0]['username'];
            $e  = $result[0]['email'];
	        $userApi  = new Api_User();
			$userApi->deleteUser($u, $e);
	        
	        $ok = $modelUser->deleteUser($id);
        }
        $this->redirect('/admin/user');
    }
}
